package com.example.gybicycle.controller.web;
import  com.alibaba.fastjson.JSON;

import com.example.gybicycle.entity.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.example.gybicycle.common.Log.SysLoging;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.util.*;
import com.example.gybicycle.service.LockService;



@Tag(name = "锁")
@RestController
@RequestMapping("/web/lock")
public class LockController {
    @Autowired
    private LockService lockService;
    @PostMapping("/get")
    @SysLoging("锁管理-查看")
    @Operation(summary = "根据id查看")
    @RequiresPermissions("lock:list")
    @ApiOperationSupport(includeParameters = {"lock.id"})
    public Result get(@RequestBody Lock lock){
      lock=lockService.get(lock.getId());
        return Result.success(lock);
    }
    @PostMapping("/list")
    @Operation(summary =  "列表查看")
    @ApiOperationSupport(includeParameters = {"page.id","page.page","page.size","page.name","lock.state"})
    @RequiresPermissions("lock:list")
    @SysLoging("锁管理-查看")
    public Result findList(@RequestBody(required=false) Lock page){
        page.setGrade(1);
        Page<Lock> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->lockService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/insert")
    @Operation(summary =  "添加")
    @RequiresPermissions("lock:insert")
    @SysLoging("锁管理-添加")
    public Result insert(@RequestBody Lock lock, HttpServletRequest request){
         LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        Lock us=new Lock();
        us.setMainId(lock.getMainId());
        us.setCid(lock.getCid());
        us.setState(1);
        int  count =lockService.count(us);
        if(count>0)
            return Result.fail("当前锁已录入");
        if(lock.getGrade().equals(2))
            lock.setState(1);
        int num=lockService.insert(lock);
         if(num==0)
             return Result.fail("添加锁失败");
         else return Result.success(lock);
    }
    @PostMapping("/update")
    @Operation(summary =  "修改")
    @RequiresPermissions("lock:update")
    @SysLoging("锁管理-修改")
    public Result update(@RequestBody Lock lock, HttpServletRequest request){
        LoginInfo loginInfo=LoginUtil.getLoginInfo(request);
        if(loginInfo==null)
            return Result.loginOut();
        Lock us=new Lock();
        us.setMainId(lock.getMainId());
        us.setCid(lock.getCid());
        us.setGrade(lock.getGrade());
         List<Lock>  count =lockService.findList(us);
        if((count.size()>1)||(count.size()==1&&!count.get(0).getId().equals(lock.getId())))
            return Result.fail("当前锁已录入");
        int num=lockService.update(lock);
        if(num==0)
            return Result.fail("修改锁失败");
        else return Result.success(lock);
    }
  @PostMapping("/delete")
    @Operation(summary =  "删除")
    @RequiresPermissions("lock:delete")
    @SysLoging("锁管理-删除")
    public Result delete(@RequestBody Lock entity){
        entity=lockService.get(entity);
        int num=0;
        if(entity.getGrade().equals(1))
            num=lockService.deleteByMain(entity);
        else
            num=lockService.delete(entity.getId());
        if(num>0)
            return Result.success("删除成功");
        else 
            return Result.fail("删除失败");
    }
     @PostMapping("deletelist")
    @Operation(summary =  "批量删除")
    @ApiOperationSupport(includeParameters = {"lock.id"})
    @RequiresPermissions("lock:delete")
    @SysLoging("锁管理-批量删除")
    public Result deletelist(@RequestBody List<String> data){
        int num=lockService.deleteByBatch(data);
         if(num>0)
             return Result.success("删除成功");
         else
             return Result.fail("删除失败"); 
    }
}